package com.group4.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.group4.rbac.entity.Permission;
import com.group4.rbac.mapper.PermissionMapper;
import com.group4.rbac.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author cloud
 * @since 2022-03-10
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 修改权限业务罗层
     * @param permission
     * @return
     */
    @Override
    public ResponseResult<Permission> update(Permission permission) {
           //数据有限性校验
        if(permission.getId()==null || permission.getId().trim().length()==0){
            return new ResponseResult<>(500,"请正确填入修改信息");
        }
        //调用方法通过id修权限
        int i = permissionMapper.updateById(permission);
        //判断是否修改成功 为0修改失败，
        if(i == 0){
            return new ResponseResult<>(501,"修改失败请仔细检检查");
        }
        //查询修改后的数据并返回 做测试用可以不返回
        QueryWrapper<Permission> qw = new QueryWrapper<>();
        qw.eq("id",permission.getId());
        Permission permission1 = permissionMapper.selectOne(qw);
        //修改成功返回200并返回修改后的数据
        return new ResponseResult<>(200,"修改成功",permission1);
    }

    @Override
    public ResponseResult<Permission> add(Permission permission) {
        //1.数据有效性校验
        if(permission == null){
            return new ResponseResult<>(500,"请正确填写数据不能为空");
        }
        //2.添加
        //3.判断是否添加成功
        //4.返回添加结果和数据
        permissionMapper.insert(permission);

        return null;
    }
}
